package com.dg.lecheng.manager.controller;

import com.dg.lecheng.api.user.model.InfoUser;
import com.dg.lecheng.api.user.service.InfoUserService;
import com.dg.lecheng.base.core.model.BaseResult;
import com.dg.lecheng.base.core.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/10/22 0022.
 */
@Api(value = "InfoUserController" ,description = "用户Controller")
@Controller
@RequestMapping("/infoUser")
public class InfoUserController extends BaseController{

    @Autowired
    private InfoUserService infoUserService;

    @ApiOperation("获取用户列表")
    @RequestMapping(value = "/getList" ,method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@ApiParam(name = "phoneNum" ,value = "用户手机号") @RequestParam(value = "phoneNum",required = false) String phoneNum,
                          @ApiParam(name = "email" ,value = "用户邮箱") @RequestParam(value = "email",required = false) String email){
        List<InfoUser> infoUsers = infoUserService.findByPhoneNumAndEmail(phoneNum,email,getPageBounds());
        return BaseResult.success(infoUsers);

    }


    @ApiOperation("获取单个用户")
    @RequestMapping(value = "get/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable int id){
        return BaseResult.success(infoUserService.get(id));
    }

    /**
     * 添加登录用户信息
     * 规则：默认密码为"666666",数据库密码MD5加密
     * 修改：用户自己修改自己的密码，单独的修改功能
     * @param infoUser
     * @return
     */
    @ApiOperation("添加用户")
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    @ResponseBody
    public Object add(@ApiParam(name = "infoUser" ,value = "用户model") @RequestBody InfoUser infoUser){
        if(infoUser.getId() == null){
            infoUserService.add(infoUser);
        }else{
            infoUserService.update(infoUser);
        }
        return BaseResult.success(null);
    }

    @ApiOperation("用户密码修改")
    @RequestMapping(value = "/updatePw" ,method = RequestMethod.POST)
    @ResponseBody
    public Object updatePw(@ApiParam(name = "newPw" ,value = "新密码") @RequestParam String newPw){
        infoUserService.updatePw(newPw);
        return BaseResult.success(null);
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "/delete/{id}" ,method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable int id){
        infoUserService.removeUserAndRelatedInfo(id);
        return BaseResult.success(null);
    }

    @ApiOperation("用户登出")
    @RequestMapping(value = "/logout" ,method = RequestMethod.GET)
    @ResponseBody
    public Object logout(){
        SecurityUtils.getSubject().logout();
        return BaseResult.success(null);
    }

}
